<?php
$cs = Yii::app()->clientScript;
//tags edits
$cs->registerCssFile(Yii::app()->request->baseUrl . "/js/jquery_ui/jquery.ui.autocomplete.css");
$cs->registerCssFile(Yii::app()->request->baseUrl . "/js/tagedit/jquery.tagedit.css");
$cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/jquery_ui/jquery-ui-1.8.6.custom.min.js", CClientScript::POS_HEAD);
$cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/tagedit/jquery.autoGrowInput.js", CClientScript::POS_HEAD);
$cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/tagedit/jquery.tagedit-1.2.0.js", CClientScript::POS_HEAD);

//auto suggest
$cs->registerCssFile(Yii::app()->request->baseUrl . "/js/jquery_ui/jquery.autosuggest.css");
$cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/jquery_ui/jquery.autosuggest.js", CClientScript::POS_HEAD);
//ckeditor
$cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/ckeditor/ckeditor.js");

$cs->registerScript('form','
         var assetPath = "'.Yii::app()->assetManager->publish(Yii::getPathOfAlias('zii.widgets.assets')).'";
            //Start ckeditor setup
            if ( typeof CKEDITOR == "undefined" ){
	            document.write("CKEditor not found." ) ;
            } else {
	            //ckeditor
                var editor = CKEDITOR.replace( "Product_content",{
                toolbar: "Full",
                height:"300",
                width:800,
                resize_maxWidth : 800,
                });
            }
            //End ckeditor setup

            $("#product-form input.tagged" ).tagedit({
                autocompleteURL: "'.Yii::app()->createUrl('product/tagsList').'",
                allowEdit: true,
                allowAdd: true
            });
            
            $("#Article_primary_category_name").autosuggest("'.Yii::app()->createUrl('articleCategory/getCategoryList').'", {
                width: 320,
                max: 0,
                highlight: false,
                scroll: true,
                scrollHeight: 300,
                formatItem: function(data, i, n, value) {
                    value = value.split(" hv@102 ");
                    // add Id to hidden field
                    if(value[3] == "none")
                        return value[1];
                    else
                        return "<table width=\"100%\" cellpadding=0 cellspacing=0><tr><td width=\"45\" align=\"center\"><img src=\'" + value[3] + "\' width=\"40\" height=\"30\" /></td><td align=\"left\">" + value[1] + "</td></tr></table>";
                },
                formatResult: function(data, value) {
                    return value.split(" hv@102 ")[2];
                }
            });
        ',
        CClientScript::POS_END
    );
$optionHtml = Common::GetOptionAction($this->action->id);
?>
<div class="form">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'product-form',
        'enableAjaxValidation' => false,
        'htmlOptions' => array('enctype'=>'multipart/form-data')
    ));
    ?>
    <input type="hidden" id="model-name" value="Product"/>
    <p class="note">The fields <span class="required">*</span> are required.</p>
    <?php //echo $form->errorSummary($model);  ?>

    <table id="Product-basic-info" class=" form-view group-info show">
        <tr class="odd">
            <th><?php echo $form->labelEx($model, 'category'); ?></th>
            <td>
                <?php echo $form->dropDownList($model, 'category', ExtensionClass::produc_cat_arr(), array('style' => 'width:389px;','tabindex'=>1)); ?>
                <?php echo $form->error($model, 'category'); ?>
            </td>
        </tr>
        <tr class="even">
            <th><?php echo $form->labelEx($model, 'category_ids'); ?></th>
            <td>
                <div id="Article-category-select" >
                        <input type="hidden" name="category_ids" id="category_ids" value="<?php echo $model->category_ids;?>" />
                        <input type="text" id="categories" onkeypress="return onKeyPress(this, event);" name="categories[]" value="" tabindex="3" size="60" class="catName" tabindex="2" />
                        <?php
                            if($model->category_ids){
                                $categories = explode(',', $model->category_ids);
                                foreach($categories as $category){
                                    $catName = Product::model()->findByPk($category)->product_name;
                                    echo '<input type="text" name="categories[]" id="'.$category.'" value="'.$catName.'" class="catName" />';
                                }
                            }
                        ?>
                    </div>
                <?php echo $form->error($model, 'category_ids'); ?>
            </td>
        </tr>
        <tr class="odd">
            <th><?php echo $form->labelEx($model, 'brand_id'); ?></th>
            <td>
                <?php echo $form->dropDownList($model, 'brand_id', ExtensionClass::brand_arr(), array('style' => 'width:389px;','tabindex'=>3)); ?>
                <?php echo $form->error($model, 'brand_id'); ?>
            </td>
        </tr>
        <tr class="even">
            <th><?php echo $form->labelEx($model, 'product_name'); ?></th>
            <td>
                <?php echo $form->textField($model, 'product_name', array('size' => 60, 'maxlength' => 300,'rel'=>'name','rev'=>$optionHtml['rev'],'tabindex'=>4)); ?>
                <?php echo $optionHtml['span']?>
                <?php echo $form->error($model, 'product_name'); ?>
            </td>
        </tr>
        <tr class="odd">
            <th><?php echo $form->labelEx($model, 'url_key'); ?></th>
            <td>
                <?php echo $form->textField($model, 'url_key', array('size' => 60, 'maxlength' => 300,'tabindex'=>5,'rel'=>'url_key','rel'=>'url_key')); ?>
                <?php echo $form->error($model, 'url_key'); ?>
            </td>
        </tr>
        <tr class="even">
            <th><?php echo $form->labelEx($model, 'product_code'); ?></th>
            <td>
                <?php echo $form->textField($model, 'product_code', array('size' => 60, 'maxlength' => 100,'tabindex'=>6)); ?>
                <?php echo $form->error($model, 'product_code'); ?>
            </td>
        </tr>
        <tr class="odd">
            <th><?php echo $form->labelEx($model, 'old_price'); ?></th>
            <td>
                <?php echo $form->textField($model, 'old_price', array('size' => 60, 'maxlength' => 100, 'class' => 'numbersOnly','tabindex'=>7)); ?>
                <?php echo $form->error($model, 'old_price'); ?>
            </td>
        </tr>
        <tr class="even">
            <th><?php echo $form->labelEx($model, 'new_price'); ?></th>
            <td>
                <?php echo $form->textField($model, 'new_price', array('size' => 60, 'maxlength' => 100, 'class' => 'numbersOnly','tabindex'=>8)); ?>
                <?php echo $form->error($model, 'new_price'); ?>
            </td>
        </tr>
        <tr class="odd">
            <th><?php echo $form->labelEx($model, 'is_hot'); ?></th>
            <td>
                <?php echo $form->checkBox($model, 'is_hot',array('tabindex'=>9)); ?>
                <?php echo $form->error($model, 'is_hot'); ?>
            </td>
        </tr>

        <tr class="even">
            <th><?php echo $form->labelEx($model, 'status'); ?></th>
            <td>
                <?php echo $form->dropDownList($model, 'status', Lvc::statusArray(), array('style' => 'width:140px;','tabindex'=>10)); ?>
                <?php echo $form->error($model, 'status'); ?>
            </td>
        </tr>
    </table>
    <div id="Product-relevant-info" class="group-info">
        <table class="form-view">
            <tr class="odd">
                <th><?php echo $form->labelEx($model, 'tagged'); ?></th>
                <td>
                    <div class="ui-widget">
                        <input type="text" id="tagNames" onkeypress="return Chienlv.onKeyPress(this, event);" name="tagName[]" tabindex="1" value="" class="tagged" />
                    <?php
                        if($model->tagged){
                            $tag = explode(',', $model->tagged);
                            $totalTags = count($tag);
                            for($i = 0; $i < $totalTags; $i++){
                                echo '<input type="text" id="'.$i.'" name="tagName[]" value="'.trim($tag[$i]).'" class="tag" />';
                            }
                        }
                    ?>
                    <?php echo $form->error($model,'tagged'); ?>
                    </div>
                </td>
            </tr>
            <tr class="even">
                <th><?php echo $form->labelEx($model, 'content'); ?></th>
                <td>
                    <?php echo $form->textArea($model, 'content', array('rows' => 6, 'cols' => 50,'tabindex'=>5)); ?>
                    <?php echo $form->error($model, 'content'); ?>
                </td>
            </tr>
            <tr class="odd">
                <th><?php echo $form->labelEx($model, 'blogspot'); ?></th>
                <td>
                    <?php echo $form->dropDownList($model, 'blogspot', Lvc::blogspotArray(), array('style' => 'width:140px;','tabindex'=>4)); ?>
                    <?php echo $form->error($model, 'blogspot'); ?>
                </td>
            </tr>
            <tr class="even">
                <th><?php echo $form->labelEx($model, 'sorder'); ?></th>
                <td>
                    <?php echo $form->textField($model, 'sorder', array('size' => 20, 'maxlength' => 20,'tabindex'=>3)); ?>
                    <?php echo $form->error($model, 'sorder'); ?>
                </td>
            </tr>
        </table>
    </div>
    <div id="Product-image-info" class="group-info grid-view">
        <table class="items" id="table-image-grid">
            <thead>
                <tr>
                    <th><?php echo Yii::t('BackEnd', 'Stt')?></th>
                    <th><?php echo Yii::t('BackEnd', 'Name')?></th>
                    <th><?php echo Yii::t('BackEnd', 'Image')?></th>
                    <th><?php echo Yii::t('BackEnd', 'Default')?></th>
                    <th>&nbsp;</th>
                </tr>
            </thead>
            <tbody>
            <?php
            $totalImage = 0;
            if($images){
                $imageIndex = $images['index'];
                $totalImage = sizeof($imageIndex);
                for($i=0; $i<$totalImage; $i++){
                $ext = substr($images[$imageIndex[$i]]['name'], strrpos($images[$imageIndex[$i]]['name'], '.'));
            ?>
                <tr class="<?php echo ($i%2==0)?"even":"odd"?>">
                    <td class="center" id="image-no-<?php echo $i+1?>">
                        <?php echo $i+1?>
                        <input type="hidden" value="<?php echo $imageIndex[$i]?>" name="imageRow[index][]">
                    </td>
                    <td class="center">
                        <input size="30" type="text" name="imageRow[<?php echo $imageIndex[$i]?>][name]" value="<?php echo $images[$imageIndex[$i]]['name']?>">
                    </td>
                    <td class="center">
                        <?php if(isset($images[$imageIndex[$i]]['id']) && $images[$imageIndex[$i]]['id'] >0):?>
                        <img src="<?php echo $model->getImageUrl($images[$imageIndex[$i]]['id'], 'small'.$ext)?>" alt="" align="middle" />
                        <input type="hidden" value="<?php echo $images[$imageIndex[$i]]['id']?>" name="imageRow[<?php echo $imageIndex[$i]?>][id]">
                        <?php endif;?>
                        <input type="file" name="imageRow[<?php echo $imageIndex[$i]?>][file]" value=""/>
                    </td>
                    <td class="center">
                        <input type="radio" name="is_default" value="<?php echo $imageIndex[$i]; ?>" <?php echo ($imageIndex[$i] == $imageDefaultIndex)?'checked="checked"':''?>/>
                    </td>
                    <td class="center btnDeleteImage" id="image-remove-row-<?php echo $imageIndex[$i]?>">
                        <img src="<?php echo Yii::app()->assetManager->publish(Yii::getPathOfAlias('zii.widgets.assets'))?>/gridview/delete.png" onclick="removeTrPrdAttr(<?php echo $imageIndex[$i]?>, <?php echo $i+1?>, 'image')" />
                    </td>
                </tr>
            <?php } //end for?>
                <input type="hidden" name="image_remove_id" value=""/>
                <input type="hidden" name="oldImageDefaultIndex" value="<?php echo $imageDefaultIndex; ?>"/>
            <?php } //endif?>
            </tbody>
        </table>
        <div class="fRight">
            <input type="hidden" value="<?php echo $totalImage; ?>" id="image-inc">
            <input type="button" class="addImg" name="addRow" value="<?php echo Yii::t('BackEnd', 'Add image')?>" onclick="Chienlv.addImage($('#image-inc'));"/>
        </div>
    </div>
    <div id="Product-seo-info" class="group-info">
        <table class="form-view">
            <tr class="even">
                <th><?php echo $form->labelEx($seo, 'title'); ?></th>
                <td>
                    <?php echo $form->textField($seo, 'title', array('size' => 100, 'maxlength' => 300,'tabindex'=>1,'rel'=>'seo_title')); ?>
                    <?php echo $form->error($seo, 'title'); ?>
                </td>
            </tr>
            <tr class="odd">
                <th><?php echo $form->labelEx($seo, 'keyword'); ?></th>
                <td>
                    <?php echo $form->textArea($seo, 'keyword', array('rows' => 1, 'cols' => 76,'tabindex'=>2,'rel'=>'seo_keywords')); ?>
                    <?php echo $form->error($seo, 'keyword'); ?>
                </td>
            </tr>
            <tr class="even">
                <th><?php echo $form->labelEx($seo, 'description'); ?></th>
                <td>
                    <?php echo $form->textArea($seo, 'description', array('rows' => 1, 'cols' => 76,'tabindex'=>3,'rel'=>'seo_description')); ?>
                    <?php echo $form->error($seo, 'description'); ?>
                </td>
            </tr>
        </table>
    </div>
    <input type="submit" style="display:none"/>
    <?php $this->endWidget(); ?>

</div><!-- form -->